<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: theme_modify.tpl.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
	defined('IN_TWCMS') || exit('Access Denied');
	include load_tpl('head');
?>
	<link href="<?=TWCMS_EDITOR?>third-party/codemirror/codemirror.css" rel="stylesheet" />
	<script src="<?=TWCMS_EDITOR?>third-party/codemirror/codemirror.js"></script>

	<div class="head">
		<dl>
			<!-- <input type="button" value="新建文件" class="but1" /> -->
			<dd class="on"><?=$this->title?></dd>
		</dl>
	</div>

	<div class="p c2">
		<div class="cc thme" >
			<?php
				if(isset($this->errTips)) {
					skyTips($this->errTips);
				}else{
			?>
			<div class="left">
				<div class="thtop cf">
					<select id="theme" class="sele1" style="width:180px;margin:0">
						<option disabled="disabled" style="background:#E6DB55">请选择要编辑的主题：</option>
						<?php foreach($this->themeAll as $dirname=>$lv) { ?>
							<option value="<?=$dirname?>"<?php if($dirname == $this->theme) echo ' selected="selected"'; ?>><?=$lv['name']?> (<?=$dirname?>)</option>
						<?php } ?>
					</select>
				</div>
				<div class="thmb">
					<ul id="tpl">
						<?php foreach($this->themeFile as $file) { if(is_array($file)) { ?>
						<li title="<?=$file['file']?>"><?=$file['name']?>(<?=$file['file']?>)</li>
						<?php }else{ ?>
						<li title="<?=$file?>"><?=$file?></li>
						<?php }} ?>
					</ul>
				</div>
			</div>

			<div class="right">
				<div class="thtop cf"><b><?=$this->themeAll[$this->theme]['name']?>：</b><span id="file"></span></div>
				<div><textarea id="code"></textarea></div>
				<div class="thrbot cf"><input id="save" type="button" value="保存" class="but1"></div>
			</div>
			<?php } ?>
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
var extArr, editor;
extArr = {htm:"htmlmixed", css:"css", js:"javascript"};
editor = CodeMirror.fromTextArea(document.getElementById("code"), {tabMode:"indent",lineNumbers:true,lineWrapping:true});

$(window).resize(function(){ setTheEditWidthHeight(); });
$(".p").css("overflow","hidden");
function setTheEditWidthHeight() {
	$("#tpl").height($(".p").height()-44);
	$(".CodeMirror-scroll").height($(".p").height()-77);
	$(".thme .right").css({"width":($(".cc").width()-200),"float":"right","margin":0});
}

$("#tpl li[title='index.htm']").addClass("on");
getTplCode($("#theme").val(), $("[title='index.htm']"));
$("#tpl li").click(function(){
	$("#tpl li").removeAttr("class");
	$(this).addClass("on");
	getTplCode($("#theme").val(), $(this));
});

$("#theme").change(function(){
	var val = $(this).val();
	if(!val) return;
	window.location.href = "<?=$this->form?>&theme="+val;
});

$("#save").click(function(){
	twAjax.postd("<?=$this->form?>&ajax=tpl_write", {'theme':$("#theme").val(),'file':$("#file").attr("title"), 'code':editor.getValue()});
});

function getTplCode(theme, obj) {
	try{
		$("#file").html(obj.html()).attr("title", obj.attr("title"));
		var file = obj.attr("title");
		var ext = getExt(file);
		var mode = !extArr[ext] ? "htmlmixed" : extArr[ext];
		twAjax.loading();
		twAjax.get("<?=$this->form?>&ajax=tpl_read&theme="+theme+"&file="+file+"&r="+time(), function(html){
			editor.toTextArea();
			editor = CodeMirror.fromTextArea(document.getElementById("code"), {"mode":mode,tabMode:"indent",lineNumbers:true,lineWrapping:true});
			editor.setValue(html);
			setTheEditWidthHeight();
			twAjax.remove();
		});
	}catch(e){
		alert("不好，又出错了！点击刷新一下！");
		window.location.reload();
	}
}

function getExt(str) {
	var lastNum = str.lastIndexOf(".");
	if(lastNum === -1) return "htm";
	return str.substr(lastNum+1);
}
</script>
</html>